

cap program drop plot_mainf_costs
program define plot_mainf_costs

	shell mkdir -p "$release/plots/mainf_costs"
	shell mkdir -p "$release/plots_tables/mainf_costs"
	
	use "$sumstats_data_path/harmonized_subs.dta", clear

        drop if cost < 0	
	replace cost = cost * 100
	
	gen lshifted_cost = cost - 10
	gen rshifted_cost = cost + 10
	gen zero_lshifted_cost = - 50
	gen zero_rshifted_cost = - 38
	gen high_lshifted_cost = 1038
	gen high_rshifted_cost = 1050

	gen zero_cost = (cost == 0)
	bysort year sg_vs_indiv best_guess_metal: egen prop_zero_cost = mean(zero_cost)
	egen tag_zero = tag(year sg_vs_indiv best_guess_metal) if zero_cost
	
	gen high_cost = (cost > 1000)
	bysort year sg_vs_indiv best_guess_metal: egen prop_high_cost = mean(high_cost)
	egen tag_high = tag(year sg_vs_indiv best_guess_metal) if high_cost
	
	set_grstyle
	
	foreach i in 2015 2016 {
		foreach k in 2 3 4 {
			local upper_limit = 1000
			grstyle linewidth plineplot 0.7
			
			preserve
			keep if year == `i' & best_guess_metal == `k'
			
			foreach j in 0 1 {
				count 
				mylabels 0(0.1)0.5, myscale(`r(N)'*@) local(yla`j')
			}

			#delimit ;
			tw
			(bar prop_zero_cost zero_lshifted_cost
				if zero_cost & tag_zero & sg_vs_indiv == 0, 
				fcolor("white") lcolor("black") barwidth(10)
				) 
			(bar prop_zero_cost zero_rshifted_cost
				if zero_cost & tag_zero & sg_vs_indiv == 1, 
				color("black") barwidth(10)
				) 
			(bar prop_high_cost high_lshifted_cost
				if zero_cost & tag_zero & sg_vs_indiv == 0, 
				fcolor("white") lcolor("black") barwidth(10)
				) 
			(bar prop_high_cost high_rshifted_cost
				if zero_cost & tag_zero & sg_vs_indiv == 1, 
				color("black") barwidth(10)
				) 
			(histogram lshifted_cost
				if cost > 0 & cost <= `upper_limit' & sg_vs_indiv == 0, 
				xtitle("monthly cost ($)") ytitle("fraction")  
				xlab(0(100)`upper_limit') 
				width(50) barwidth(19) frac 
				fcolor("white") lcolor("black") 
				) 
			(histogram rshifted_cost
				if cost > 0 & cost <= `upper_limit' & sg_vs_indiv == 1, 
				xtitle("monthly cost ($)") ytitle("fraction") 
				xlab(0(100)`upper_limit')  
				width(50) barwidth(19) frac 
				color("black") ) 
		
			,legend(off) ylab(0(0.1)0.5)
			xsize(3) ysize(2)
			;
			#delimit cr
			
			graph export $release/plots/mainf_costs/`i'_`k'.eps, replace 

			#delimit ;
			tw
			(kdensity cost 
				if cost > 0 & cost <= `upper_limit' & sg_vs_indiv == 0, 
				xtitle("monthly cost ($)") ytitle("density")   
				xlab(0(100)`upper_limit')  ylab(0(0.002)0.006)
				color("white") 
				) 
			(kdensity cost
				if cost > 0 & cost <= `upper_limit' & sg_vs_indiv == 1, 
				xtitle("monthly cost ($)") ytitle("density") 
				xlab(0(100)`upper_limit')  ylab(0(0.002)0.006)   
				color("black") ) 
		
			,legend(off) 
			xsize(3) ysize(2)
			;
			#delimit cr
			
			graph export $release/plots/mainf_costs/kdens_`i'_`k'.eps, replace 

			
			collapse (count) obs = cost ///
				 (mean) mean = cost ///
				 (sd) std_dev = cost ///
				 (min) min = cost ///
				 (max) max = cost, ///
				 by(sg_vs_indiv year best_guess_metal)
			ren (sg_vs_indiv best_guess_metal) (market metal)
			order market year metal
			export delimited $release/plots_tables/mainf_costs/`i'_`k'.csv, replace
			restore
		}
	}
	
	
	* Legend Black
	#delimit ;
	twoway 
		(bar prop_zero_cost zero_lshifted_cost if zero_cost & tag_zero & sg_vs_indiv == 0 , 
				fcolor("white") lcolor("black") barwidth(20))
		(bar prop_zero_cost zero_lshifted_cost if zero_cost & tag_zero & sg_vs_indiv == 1 ,  
				color("black") barwidth(20))
		,legend(label(1 "Individual market") label(2 "Small group market") 
		nobox region(lstyle(none)) rows(1) ring(0) pos(0)) yscale(off) xscale(off)
		
	;
	#delimit cr
	gr_edit .plotregion1.draw_view.setstyle, style(no)
	graph export $release/plots/mainf_costs/legend.eps, replace 
 
 	* Legend Grey
	#delimit ;
	twoway 
		(bar prop_zero_cost zero_lshifted_cost if zero_cost & tag_zero & sg_vs_indiv == 0 , 
				fcolor("white") lcolor("black") barwidth(20))
		(bar prop_zero_cost zero_lshifted_cost if zero_cost & tag_zero & sg_vs_indiv == 1 ,  
				fcolor("gray") lcolor("black") barwidth(20))
		,legend(label(1 "Individual market") label(2 "Small group market") 
		nobox region(lstyle(none)) rows(1) ring(0) pos(0)) yscale(off) xscale(off)
		
	;
	#delimit cr
	gr_edit .plotregion1.draw_view.setstyle, style(no)
	graph export $release/plots/mainf_costs/legend_grey.eps, replace 
	
end
